//
//  HomeCollectionViewCell.m
//  cityCar
//
//  Created by 常宽 on 14/10/29.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HomeCollectionViewCell.h"
#import "AutoScrollView.h"
#import "PullView.h"
#import "CKPhoto.h"

@interface HomeCollectionViewCell ()

@property (nonatomic, retain)NSMutableArray *photoArray;
@property (nonatomic, retain)NSMutableArray *CKPhotoArray;

@end

@implementation HomeCollectionViewCell

- (void)dealloc
{
    [_photo release];
    [_photoArray release];
    [_CKPhotoArray release];
    [_waterfallView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.photoArray = [NSMutableArray array];
        self.CKPhotoArray = [NSMutableArray array];
        
        self.waterfallView = [[PullView alloc]init];
        [self.contentView addSubview:_waterfallView];
        [_waterfallView release];

        [self netWork];
    }
    return self;
}

- (void)netWork
{
    NSString *str = @"http://mrobot.pcauto.com.cn/v2/cms/channels/1?pageNo=1&pageSize=20&v=4.0.0";
    [Connection networkHandlerWith:str block:^(id result) {
        NSArray *array = [result objectForKey:@"focus"];
        for (NSDictionary *dic in array) {
            CKPhoto *ckphoto = [[CKPhoto alloc]init];
            [ckphoto setValuesForKeysWithDictionary:dic];
            [self.CKPhotoArray addObject:ckphoto];
            
            [self.photoArray addObject:ckphoto.image];
            [ckphoto release];
            
        }
        [self.photo setImageUrls:self.photoArray];
        
    }];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat w = self.contentView.frame.size.width - 10;
    CGFloat h = self.contentView.frame.size.height;
    self.photo = [[AutoScrollView alloc]initWithFrame:CGRectMake(5, 5, w, h / 3)];
    self.photo.showPageControl = NO;
    self.photo.timeInterval = 5;
    [self.photo setTarget:self action:@selector(photoShowAction:)];
    [self.contentView addSubview:_photo];
    [_photo release];
    
    self.waterfallView.frame = CGRectMake(5, h / 3 + 10, w, h / 3 * 2 - 5);
    self.waterfallView.string = @"http://mrobot.pcauto.com.cn/v2/cms/channels/1?pageNo=1&pageSize=20&v=4.0.0";

//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
//    Reachability *hostReach = [Reachability reachabilityWithHostName:@"www.google.com"];//可以以多种形式初始化
//    [hostReach startNotifier];
    
}
// 传出轮播的id
- (void)photoShowAction:(AutoScrollView *)photo
{
    CKPhoto *ckph = [self.CKPhotoArray objectAtIndex:[photo pageNo]];
    self.shuffling(ckph);
}
//#pragma mark 判断网络状态
//// 连接改变
//- (void) reachabilityChanged: (NSNotification* )note
//{
//    Reachability* curReach = [note object];
//    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
//    [self updateInterfaceWithReachability: curReach];
//}
//
////处理连接改变后的情况
//- (void) updateInterfaceWithReachability: (Reachability*) curReach
//{
//    //对连接改变做出响应的处理动作。
//    NetworkStatus status = [curReach currentReachabilityStatus];
//    
//    if (status != NotReachable) {
//        [self netWork];
//    }
//    
//}
//
//#pragma mark -


@end
